
export default {
  state: {
    //校区列表
    campusList: [],
    //楼栋列表
    buildingList:[]
  },
    // 监听state更新
    getters: {
      //---------------处理campusList 校区------------------
      /*
     [
       {
          label: "城市学院",
          value: "城市学院",
          children:[
          {label: "主校区" value: "65789057896547987"}]
        },
      ...
     ]
      * */
      changeCampusList(state) {
        //将获取数据内的 "id"改为"value" "campusName"改为"label"
        let g = JSON.stringify(state.campusList).replace(/id/g,"value").replace(/campusName/g, "label");
        const h = JSON.parse(g);
        let a = [];
        for (let item in h) {
          a.push({label: item,value:item, children: h[item]});
        }
        return a;
      },


      //---------------处理schoolList 学校------------------
      SchoolNameList(state){
          let nameList =[];
          const arrList = state.campusList;
        /*
        * [
        *   {label: "城市学院" value: "城市学院"},
        *   ...
        * ]
        * */
        for(let i in arrList){
          nameList.push({
            label:i,
            value:i
          });
        }
        return nameList;
      },
      //通过学校名选择校区
      campusArray(state,getter){
        return (schoolName)=>{
        const goal = getter.changeList.find(i=>{return i.label == schoolName});
          // [{label: "主校区" ,value: "65789057896547987"},...]
          return goal.children;
        }
    },
      changeBuildingList(state){
        const building = state.buildingList;
        let list = [];
        for (let keys in building){
          let {buildingName:label,buildingId:value} = building[keys];
          list.push({label,value});
        }
        /*
        [
          {label: "学1" value: "65789057896547987"},
          ...
        ]
        * */
          return list;
      },
      //校区数据回显
      campus_echo:(state)=>(campusName)=> {
        const campus = state.campusList;
        for (let item in campus) {
          let arr = campus[item];
          let res = arr.filter((i) => {
            return i.campusName === campusName;
          });
          if (res[0]) {
            return [`${item}`, `${res[0].id}`]
          }
        }
      }
    },
    // 操作state
    mutations: {
    //获取校区
      getCampus (state, value) {
        state.campusList = value;
      },

      //获取楼栋列表
      getBuilding(state,value) {
        state.buildingList = value;
      }
    },
    //state异步处理
    actions: {}

}
